﻿using System;
using System.Xml;
using System.Windows.Forms;
using ENAMonitor.ENAMonitorUpdate;
using ENAMonitor.ServiceReference1;
using System.ComponentModel;

namespace ENAMonitor
{
    public partial class LoginIn : Form
    {
        ViewENAStateSoapClient vsc = new ViewENAStateSoapClient("ViewENAStateSoap");
        ENAMonitorUpdateSoapClient esc = new ENAMonitorUpdateSoapClient("ENAMonitorUpdateSoap");
        private static AuthHeaderGB auth = new AuthHeaderGB();
        private BackgroundWorker backgroundWorker1;
        private static string username, password;

        public LoginIn()
        {
            InitializeComponent();
            backgroundWorker1 = new BackgroundWorker();
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        }

        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                updatecheck();
            }
            catch
            {
                MessageBox.Show("连接服务器失败，无法更新！");
            }
        }

        private void LoginIn_Load(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            username = this.textBox1.Text.Trim();
            password = this.textBox2.Text.Trim();
            try
            {
                if (!vsc.ValidUser(username, password))
                {
                    MessageBox.Show("无效的用户名或密码！");
                }
                else if (!vsc.ValidRole(username))
                {
                    MessageBox.Show("没有权限的用户！");
                }
                else
                {
                    Form1 f1 = new Form1();
                    Program.context.MainForm = f1;
                    f1.Show();
                    this.Dispose();
                    f1.Activate();
                }
            }
            catch
            {
                MessageBox.Show("连接服务器失败，无法登录！");
            }
        }

        private void updatecheck()
        {
            auth.Username = username;
            auth.Password = password;
            string nVer = esc.GetVer();
            //MessageBox.Show(nVer);
            //MessageBox.Show(Application.ProductVersion);
            if (Application.ProductVersion.CompareTo(nVer) < 0)
            {
                //MessageBox.Show("检测到软件有新版本，马上更新。");
                update();
            }
        }

        public void update()
        {
            //MessageBox.Show("正在检查更新，请稍候。。。");
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            //MessageBox.Show(esc.GetUpdateData(auth).OuterXml.Substring(0, 1000));
            doc.LoadXml(esc.GetUpdateData(auth).OuterXml);
            doc.Save(Application.StartupPath + @"\Update.xml");
            System.Diagnostics.Process.Start(Application.StartupPath + @"\Update.exe");
            Close();            
            Application.Exit();
        }
    }
}
